﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows;
using Wpf_Examples.Models;

namespace Wpf_Examples.Tools
{
    public class CreateParticleHelper
    {
        /// <summary>
        /// 粒子的路径
        /// </summary>
        private Geometry particleGeometry;

        /// <summary>
        /// 粒子总数
        /// </summary>
        private int totalCount = 200;

        /// <summary>
        /// 粒子最小尺寸
        /// </summary>
        private static int sizeMin = 5;

        /// <summary>
        /// 粒子最大尺寸
        /// </summary>
        private int sizeMax = 10;

        /// <summary>
        /// 随机数生成粒子大小信息
        /// </summary>
        private Random rand;

        /// <summary>
        /// 存放粒子集合
        /// </summary>
        private List<Particle> particleList;

        /// <summary>
        /// 粒子绘制的画布
        /// </summary>
        private Canvas particleCanvas;


        public CreateParticleHelper(Geometry _path, int _maxRadius, int _particleCount, Canvas _containerParticles)
        {
            particleGeometry = _path;
            totalCount = _particleCount;
            sizeMax = _maxRadius;
            particleCanvas = _containerParticles;
            rand = new Random();
            particleList = new List<Particle>();
            InitParticle();
        }

        /// <summary>
        /// 初始化粒子
        /// </summary>
        private void InitParticle()
        {
            //清空粒子队列
            particleList.Clear();
            particleCanvas.Children.Clear();

            //生成粒子
            for (int i = 0; i < totalCount; i++)
            {
                double size = rand.Next(sizeMin, sizeMax + 1);
                while (true)
                {
                    Point point = new Point(rand.Next((int)particleGeometry.Bounds.Left, (int)particleGeometry.Bounds.Right), rand.Next((int)particleGeometry.Bounds.Top, (int)particleGeometry.Bounds.Bottom));
                    if (particleGeometry.FillContains(point, 2, ToleranceType.Absolute))
                    {
                        Particle particle = new Particle
                        {
                            Shape = new Ellipse
                            {
                                Width = size,
                                Height = size,
                                Stretch = System.Windows.Media.Stretch.Fill,
                                Fill = GetColorBurshRandom(),
                            },
                            Position = point,
                        };
                        SetParticleSizeAnimation(particle.Shape);
                        particleList.Add(particle);
                        Canvas.SetLeft(particle.Shape, particle.Position.X);
                        Canvas.SetTop(particle.Shape, particle.Position.Y);
                        particleCanvas.Children.Add(particle.Shape);
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 设置粒子大小动画
        /// </summary>
        private void SetParticleSizeAnimation(Ellipse ellipse)
        {
            Storyboard sb = new Storyboard();
            //动画完成事件 再次设置此动画
            sb.Completed += (S, E) =>
            {
                SetParticleSizeAnimation(ellipse);
            };
            int size = rand.Next(sizeMin, sizeMax + 1);
            int time = rand.Next(100, 1000);
            DoubleAnimation animationX = new DoubleAnimation(size, new Duration(TimeSpan.FromMilliseconds(time)));
            DoubleAnimation animationY = new DoubleAnimation(size, new Duration(TimeSpan.FromMilliseconds(time)));
            Storyboard.SetTarget(animationX, ellipse);
            Storyboard.SetTarget(animationY, ellipse);
            Storyboard.SetTargetProperty(animationX, new PropertyPath("Width"));
            Storyboard.SetTargetProperty(animationY, new PropertyPath("Height"));
            sb.Children.Add(animationX);
            sb.Children.Add(animationY);
            sb.Begin();
        }

        /// <summary>
        /// 获取随机颜色画刷，可以根据自己需求修正该
        /// </summary>
        private SolidColorBrush GetColorBurshRandom()
        {
            byte r = (byte)rand.Next(180, 255);
            byte g = (byte)rand.Next(10, 80);
            byte b = (byte)rand.Next(10, 80);
            return new SolidColorBrush(Color.FromArgb(125, r, g, b));
        }

    }
}
